<template>
  <div class="params">
    <Breadcrumb :params1="'商品管理'" :params2="'分类参数'"></Breadcrumb>
    <el-card class="box-card">
      <el-alert
        title="注意：只允许为第三级分类设置相关参数！"
        type="warning"
        show-icon
      >
      </el-alert>
      <span>选择商品分类：</span>
      <el-cascader
        v-model="value"
        :options="options"
        :props="props"
        @change="handleChange"
      ></el-cascader>
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="动态参数" name="many">
          <el-button type="primary" :disabled="disabled">添加参数</el-button>
          <el-table :data="tableData" border style="width: 100%">
            <el-table-column type="expand" width="60">
              <template slot-scope="scope">
                <span class="tag" v-show="!scope.row.attr_vals == [' ']">
                  <el-tag
                    :key="index"
                    v-for="(tag, index) in scope.row.attr_vals"
                    closable
                    :disable-transitions="false"
                    @close="handleClose(scope.row, index)"
                  >
                    {{ tag }}
                  </el-tag>
                </span>
                <el-input
                  class="input-new-tag"
                  v-if="inputVisible"
                  v-model="inputValue"
                  ref="saveTagInput"
                  size="small"
                  @keyup.enter.native="handleInputConfirm(scope.row)"
                  @blur="handleInputConfirm(scope.row)"
                >
                </el-input>
                <el-button
                  v-else
                  class="button-new-tag"
                  size="small"
                  @click="showInput"
                  >+ New Tag</el-button
                >
              </template>
            </el-table-column>
            <el-table-column type="index" label="#" width="60">
            </el-table-column>
            <el-table-column prop="attr_name" label="参数名称">
            </el-table-column>
            <el-table-column label="操作">
              <el-button type="primary" icon="el-icon-edit">编辑</el-button>
              <el-button type="danger" icon="el-icon-delete">删除</el-button>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="静态参数" name="only">
          <el-button type="primary" :disabled="disabled">添加属性</el-button>
          <el-table :data="tableData" border style="width: 100%">
            <el-table-column type="expand" width="60">
              <template slot-scope="scope">
                <pre>{{ scope.row.attr_vals }}</pre>
                <span class="tag">
                  <el-tag
                    :key="index"
                    v-for="(tag, index) in scope.row.attr_vals"
                    closable
                    :disable-transitions="false"
                    @close="handleClose(scope.row, index)"
                  >
                    {{ tag }}
                  </el-tag>
                  <el-input
                    class="input-new-tag"
                    v-if="inputVisible"
                    v-model="inputValue"
                    ref="saveTagInput"
                    size="small"
                    @keyup.enter.native="handleInputConfirm(scope.row)"
                    @blur="handleInputConfirm(scope.row)"
                  >
                  </el-input>
                  <el-button
                    v-else
                    class="button-new-tag"
                    size="small"
                    @click="showInput"
                    >+ New Tag</el-button
                  >
                </span>
              </template>
            </el-table-column>
            <el-table-column type="index" label="#" width="60">
            </el-table-column>
            <el-table-column prop="attr_name" label="属性名称">
            </el-table-column>

            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  type="primary"
                  icon="el-icon-edit"
                  @click="Edid(scope.row)"
                  >编辑</el-button
                >
                <el-button type="danger" icon="el-icon-delete">删除</el-button>
                <el-dialog
                  title="请输入参数名称"
                  :visible.sync="dialogVisible"
                  width="30%"
                  :before-close="handleCloseEdit"
                >
                  <el-form
                    :model="ruleForm"
                    :rules="rules"
                    ref="ruleForm"
                    label-width="100px"
                    class="demo-ruleForm"
                  >
                    <el-form-item label="静态属性" prop="name">
                      <el-input v-model="ruleForm.name" style="width : 300px"></el-input>
                    </el-form-item>
                  </el-form>
                  <span slot="footer" class="dialog-footer">
                    <el-button @click="dialogVisible = false">取 消</el-button>
                    <el-button
                      type="primary"
                      @click="dialogVisibleEdit(scope.row)"
                      >确 定</el-button
                    >
                  </span>
                </el-dialog>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>
           
<script>
import {
  goodscategories_api,
  dynamic_api,
  static_api,
  addcategories_api,
  editcategories_api,
} from '@/api/index'

export default {
  data() {
    return {
      value: [],

      options: [],
      props: {
        label: 'cat_name',
        value: 'cat_id',
        children: 'children',
        expandTrigger: 'hover',
      },
      params: {
        pagenum: 1,
        pagesize: 10,
      },
      activeName: 'many',
      tableData: [],

      // tag 里的数据处理
      dynamicTags: [],

      inputVisible: false,
      inputValue: '',

      dialogVisible: false,

      ruleForm: {
        name: '',
      },
      rules: {
        name: [
          { required: true, message: '请输入参数名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ],
      },
    }
  },
  methods: {
    handleChange(value) {
      this.getcateList()
    },

    async getcateList() {
      if (this.cateid) {
        var { data: res } = await dynamic_api({
          id: this.cateid,
          sel: this.activeName,
        })
        res.data.forEach((item) => {
          item.attr_vals = item.attr_vals.split(' ')
        })

        this.tableData = res.data
      }
    },

    async getCategories() {
      var { data: res } = await goodscategories_api(this.params)

      var classList = res.data.result
      // console.log(res.data.result);
      this.options = classList
    },
    handleClick() {
      this.handleChange(this.value)
    },

    // tag 上的一系列处理函数
    async handleClose(row, index) {
      row.attr_vals.splice(index, 1)
      row.attr_vals = row.attr_vals.join(' ')

      var { data: res } = await addcategories_api({
        id: this.cateid,
        attrId: row.attr_id,
        attr_name: row.attr_name,
        attr_sel: this.activeName,
        attr_vals: row.attr_vals,
      })

      if (res.meta.status == 200) {
        this.$message.success(res.meta.msg)
        row.attr_vals = res.data.attr_vals.split(' ')
      } else {
        this.$message.error(res.meta.msg)
      }
    },

    showInput() {
      this.inputVisible = true
      this.$nextTick((_) => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },

    // 失焦后触发
    async handleInputConfirm(row) {
      // console.log(row);

      let inputValue = this.inputValue

      if (!inputValue.trim() == '') {
        row.attr_vals.push(inputValue)

        row.attr_vals = row.attr_vals.join(' ')

        // console.log(row.attr_vals);

        var { data: res } = await addcategories_api({
          id: this.cateid,
          attrId: row.attr_id,
          attr_name: row.attr_name,
          attr_sel: this.activeName,
          attr_vals: row.attr_vals,
        })

        // console.log(res)

        if (res.meta.status == 200) {
          this.$message.success(res.meta.msg)
          row.attr_vals = res.data.attr_vals.split(' ')
        } else {
          this.$message.error(res.meta.msg)
        }
      }

      this.inputVisible = false
      this.inputValue = ''
    },

    Edid(row) {
      this.dialogVisible = true
      this.ruleForm.name = row.attr_name
    },

    async dialogVisibleEdit(row) {
      var { data: res } = await editcategories_api({
        id: this.cateid,
        attrId: row.attr_id,
        attr_name: this.ruleForm.name,
        attr_sel: this.activeName,
      })
      console.log(res)
    },

    handleCloseEdit(done) {
      this.$confirm('确认关闭？')
        .then((_) => {
          done()
        })
        .catch((_) => {})
    },
  },
  computed: {
    disabled() {
      if (this.value.length != 3) {
        return true
      } else {
        return false
      }
    },
    cateid() {
      if (this.value.length == 3) {
        return this.value[2]
      } else {
        return false
      }
    },
  },
  created() {
    this.getCategories()
  },
}
</script>
<style scoped lang='less'>
.box-card {
  width: 95%;
  .el-alert {
    margin-bottom: 20px;
  }
  .el-tag {
    margin: 15px;
  }
  .el-input {
    width: 80px;
  }
}
</style>
